//
//  FriendListViewController.m
//  YiXiProject
//
//  Created by iMac on 2019/7/25.
//  Copyright © 2019 iMac. All rights reserved.
//

#import "FriendListViewController.h"

#import "FriendTopView.h"           // topView
#import "FriendOneTableViewCell.h"  // cell - 第一分区
#import "FriendTwoTableViewCell.h"  // cell - 第二分区
#import "SpeekViewController.h"     // 聊天界面

@interface FriendListViewController () <UITableViewDelegate,UITableViewDataSource,ClickfriendBtnDelegate,FriendCollectDelegate>
{
    NSArray *arrHead;
}

@property (nonatomic,strong) FriendTopView *topView;  // topView属性
@property (nonatomic,strong) UITableView *table;      // table

@property (nonatomic,strong) UITableView *rightTable;

@property (nonatomic,strong) NSArray *arrNewList,*arrOldList,*arrRightList;
@property (nonatomic,strong) NSArray *typeImgArr;      //

@end

@implementation FriendListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorAlphe(245, 245, 245, 1);
    
    arrHead = @[@"New Matches",@"Messages"];
    _typeImgArr = @[@"inforSC",@"inforLike",@"inforNoLike"];
    
    [self creatTopView];
    
    [self.view addSubview:self.table];
    
    [self.view addSubview:self.rightTable];
    
    [self requestDats];
    [self requestRightDatas];
}


-(void)requestDats
{
    kWeakSelf(self);
    [HttpDatas requestFriendDataToken:@"f2e0c88f-7d32-3464-9cc5" success:^(id  _Nonnull json) {
        
        if([json isKindOfClass:[NSDictionary class]])
        {
            if([json[@"status"]integerValue] == 200)
            {
                weakself.arrNewList = json[@"data"][@"new_user_list"];
                weakself.arrOldList = json[@"data"][@"old_user_list"];
                
                [weakself.table reloadData];
            }
        }
        
    } failure:^(NSError * _Nonnull err) {
        
    }];
}

-(void)requestRightDatas
{
    kWeakSelf(self);
    [HttpDatas requestCollectFriendDataToken:@"f2e0c88f-7d32-3464-9cc5" success:^(id  _Nonnull json) {
        
        if([json isKindOfClass:[NSDictionary class]])
        {
            weakself.arrRightList = json[@"data"];
            
            [weakself.rightTable reloadData];
        }
        
    } failure:^(NSError * _Nonnull err) {
        
    }];
}

// topView 初始化
-(void)creatTopView
{
    _topView = [[FriendTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, navCurentHeight+120)];
    _topView.backgroundColor = [UIColor whiteColor];
    _topView.delegate = self;
    [self.view addSubview:_topView];
}

#pragma mark -- ClickfriendBtnDelegate
-(void)clickFriendButtonTag:(NSInteger)tag
{
    switch (tag) {
        case 100:
            [self.navigationController popViewControllerAnimated:YES];
            break;
            
        case 101:
            NSLog(@"mess");
            [self leftBtn];
            break;
            
        case 102:
            NSLog(@"feed");
            [self rightBtn];
            break;
            
        default:
            break;
    }
}

// 显示左侧按钮table
-(void)leftBtn
{
    _table.hidden = NO;
    _rightTable.hidden = YES;
}
// 显示右侧按钮table
-(void)rightBtn
{
    _table.hidden = YES;
    _rightTable.hidden = NO;
}

// 初始化table
-(UITableView *)table
{
    if(!_table)
    {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, self.topView.bounds.size.height+10, SCREEN_WIDTH, SCREEN_HEIGHT-self.topView.bounds.size.height-5) style:UITableViewStyleGrouped];
        _table.delegate = self;
        _table.dataSource = self;
        _table.backgroundColor = ColorAlphe(245, 245, 245, 1);
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.tag = 100;
        _table.hidden = NO;
    }
    return _table;
}

// 初始化右侧table
-(UITableView *)rightTable
{
    if(!_rightTable)
    {
        _rightTable = [[UITableView alloc]initWithFrame:CGRectMake(0, self.topView.bounds.size.height+10, SCREEN_WIDTH, SCREEN_HEIGHT-self.topView.bounds.size.height-5) style:UITableViewStyleGrouped];
        _rightTable.delegate = self;
        _rightTable.dataSource = self;
        _rightTable.backgroundColor = ColorAlphe(245, 245, 245, 1);
        _rightTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _rightTable.hidden = YES;
    }
    return _rightTable;
}

// 分区数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  tableView.tag == 100 ? arrHead.count : 1;
}

// 每分区行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView.tag == 100)
    {
        return section == 0 ? 1 : self.arrOldList.count;
    }
    else
    {
        return self.arrRightList.count;
    }
    
    
}

// 每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView.tag == 100)
    {
        return indexPath.section == 0 ? SCREEN_WIDTH/3+20 : 100;
    }
    else
    {
        return 100;
    }
    
    
}
// 分区头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return tableView.tag == 100 ? 45 : 0.01;
}
// 分区页脚高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

// 分区头部视图显示
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headViews = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45)];
    headViews.backgroundColor = WhiteColor;

    UILabel *headL = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH-30, 40)];
    headL.text = [NSString stringWithFormat:@"%@",arrHead[section]];
    headL.textColor = ColorAlphe(178,34,34, 1);
    headL.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    [headViews addSubview:headL];

    return tableView.tag == 100 ? headViews : nil;
}

// 分区页脚视图显示
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

// 每行显示内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView.tag == 100)
    {
        if(indexPath.section == 0)
        {
            static NSString *cellid = @"cellid";
            FriendOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if(!cell)
            {
                cell = [[FriendOneTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
            
            cell.delegate = self;
            
            cell.collList = self.arrNewList;
            [cell.collectV reloadData];
            
            return cell;
        }
        else
        {
            static NSString *cellid = @"cellid1";
            FriendTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if(!cell)
            {
                cell = [[FriendTwoTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
            }
            
            [cell.headImg sd_setImageWithURL:[NSURL URLWithString:self.arrOldList[indexPath.row][@"photo"]] placeholderImage:nil];
            cell.nameLab.text = self.arrOldList[indexPath.row][@"name"];
            cell.detLabel.text = self.arrOldList[indexPath.row][@"message"];
            
            return cell;
        }
    }
    else
    {
        static NSString *cellid = @"cellid2";
        FriendTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if(!cell)
        {
            cell = [[FriendTwoTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
        }
        
        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:_arrRightList[indexPath.row][@"photo"]] placeholderImage:nil];
        cell.nameLab.text = _arrRightList[indexPath.row][@"name"];
        cell.detLabel.text = _arrRightList[indexPath.row][@"time"];
        
        // 当前cell 来源类型。（收藏、喜欢、聊天）
        NSInteger index = [self.arrRightList[indexPath.row][@"type"] integerValue];
        cell.typeImg.hidden = NO;
        cell.typeImg.image = [UIImage imageNamed:_typeImgArr[index-1]];
        
        return cell;
    }    
}

// 点击每行时响应
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(tableView.tag == 100)
    {
        SpeekViewController *speakVC = [[SpeekViewController alloc]init];
        
        [self.navigationController pushViewController:speakVC animated:YES];
    }
    else
    {
        NSLog(@"%ld",indexPath.row);
    }
    
    
}
// 点击水平滑动王哥响应
-(void)clickFriendCollectiomItemTag:(NSInteger)tag
{
    SpeekViewController *speakVC = [[SpeekViewController alloc]init];
    
    [self.navigationController pushViewController:speakVC animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
